package com.hmall.order.listener;


import com.hmall.order.OrderDto;
import com.hmall.order.service.impl.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * 监听失效订单
 */


@Component
public class OrderListener {

    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    @Autowired(required = false)
    private OrderService orderService;

    @GlobalTransactional //全局事务
    @RabbitListener(queues = "dl.queue")
    public void OrderMsg(OrderDto orderDto) {

        //判断订单状态是否支付
        if ((orderDto.getStatus()) == 1) {

            //订单未支付
            //通过MQ远程调用，根据购买数量恢复库存
            rabbitTemplate.convertAndSend("item.order", "recover", orderDto);

            //根据订单id，修改订单状 态为交易失败，修改订单交易关闭时间时间
            Date date = new Date();
            orderService.updateOrderStatusAndCloseTime(orderDto.getId(),date);
        }
    }
}
